package stock.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Component
public class JedisUtil {
    @Autowired
    JedisPool jedisPool;

    private Jedis jedis = null;

    /**
     * 获取一个Jedis实例
     */
    public Jedis getInstance(Integer database) {
        jedis = jedisPool.getResource();
        jedis.select(database); // 选择存储库，单机版默认为db(0)
        return jedis;
    }

    /**
     * 回收Jedis实例
     */
    public void close() {
        if (jedis != null && jedisPool != null) {
            jedis.close();
        }
    }

    //===========================================String=============
    /**
     * 根据key获取Value
     */
    public String get(String key) {
        return jedis.get(key);
    }


    /**
     * 添加键值对
     */
    public String set(String key, String value) {
        // jedie.set(key, value, "NX", "EX", 1800); // 添加key设置TTL
        return jedis.set(key, value);
    }

    /**
     * 删除一个或多个key
     */
    public Long del(String... keys) {
        return jedis.del(keys);
    }

    /**
     * 匹配key删除
     * @param pattern
     */
    public void delPattern(String pattern){
        try {
            Set<String> keys = jedis.keys(pattern);
            if(keys !=null && keys.size()>0){
                Iterator<String> it = keys.iterator();
                while(it.hasNext()){
                    String key = it.next();
                    jedis.del(key);
                }
            }
        }catch (Exception e){
            throw e;
        }finally {
            jedis.close();
        }
    }
    //===========================================Set=============


    //===========================================Hash=============

    /**
     * 设置hash值
     * @param key
     * @param hk
     * @param hv
     * @return
     */
    public Long hset(String key, String hk, String hv){
        return jedis.hset(key,hk,hv);
    }

    /**
     *
     * @param key
     * @param hashMap
     * @return
     */
    public String hmset(String key, Map<String,String> hashMap){
        return jedis.hmset(key,hashMap);
    }

    /**
     * java对象序列化后存入redis
     */
    public byte[] objectSerialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * byte反序列化为java对象
     */
//    public Object deObjectSerialize(byte[] bytes) {
//        ObjectIutputStream ois = null;
//        ByteArrayIutputStream bais = null;
//        try {
//            bais = new ByteArrayIutputStream(bytes);
//            ois = new ObjectIutputStream(bais);
//            return ois.readObject();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (ois != null) {
//                    ois.close();
//                }
//                if (bais != null) {
//                    bais.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
//    }
}
